#include "stdafx.h"
#include "ZNetworkGenericMessage.h"
#include "ZMessageHandler.h"
#include "ZStream.h"
#include "ZUtility.h"

namespace	Z	{

//------------------------------------------------------------------------------------------------------------------------
NetworkGenericMessage::NetworkGenericMessage(NetworkId kNetworkId, size_t kSize)
:	NetworkMessage(kNetworkId),
	mStream(new Stream(kSize))
{
}

NetworkGenericMessage::~NetworkGenericMessage()
{
	deletePtr(mStream);
}

void NetworkGenericMessage::pump(MessageHandler& kMessageHandler)
{
	kMessageHandler.handle(*mStream, getNetworkId());
}

Stream* NetworkGenericMessage::detach()
{
	Stream* pStream	= mStream;
	mStream	= nullptr;
	return pStream;
}

byte_t* NetworkGenericMessage::data()	const
{
	return mStream->getReadableBuffer();
}

size_t NetworkGenericMessage::size()	const
{
	return mStream->getReadableSize();
}

void NetworkGenericMessage::write(const void* data, size_t kSize)
{
	mStream->write(data, kSize);
}

byte_t* NetworkGenericMessage::getBuffer()	const
{
	return mStream->getBuffer();
}

void NetworkGenericMessage::onWritten(size_t kSize)
{
	mStream->onWritten(kSize);
}


}